package io.jsen.meta.diff.feature.parser;

import io.jsen.meta.diff.feature.FieldControl;
import io.jsen.meta.diff.field.MetaFieldDefine;

/**
 * @author jsen
 * @date 2021/09/18
 **/
public class ProxyDefaultFieldControl implements FieldControl {
    private final FieldControl trueControl;
    private final FieldControl defaultControl;

    public ProxyDefaultFieldControl(FieldControl trueControl) {
        this.trueControl = trueControl;
        this.defaultControl = new DefaultFieldControl();
    }

    @Override
    public <T> boolean isDelVal(MetaFieldDefine metaFieldDefine, T val) {
        if (trueControl.isDelVal(metaFieldDefine, val)) {
            return true;
        }
        return defaultControl.isDelVal(metaFieldDefine, val);
    }

    @Override
    public <T> T getDelVal(MetaFieldDefine metaFieldDefine) {
        try {
            T res = trueControl.getDelVal(metaFieldDefine);
            if (res != null) {
                return res;
            }
        } catch (Exception ignore) {
        }
        return defaultControl.getDelVal(metaFieldDefine);
    }
}
